package com.netease.rtcdemo.screen;

import static android.support.constraint.Constraints.TAG;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.res.Configuration;
import android.os.IBinder;
import android.util.Log;

import com.netease.rtcdemo.SimpleScreenShareService;
import com.netease.rtcdemo.multiChatActivity;

public class ScreenCaptureService {

     private class ScreenShareServiceConnection implements ServiceConnection {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder service) {

            if (service instanceof SimpleScreenShareService.ScreenShareBinder) {
                mScreenService = ((SimpleScreenShareService.ScreenShareBinder) service).getService();
                Log.i(TAG, "onServiceConnect");
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            mScreenService = null;
        }
    }
    // 在类加载时就创建单例实例
    private static final ScreenCaptureService INSTANCE = new ScreenCaptureService();
    public ScreenShareServiceConnection mServiceConnection;
    private Context mContext;
    public SimpleScreenShareService mScreenService;
    private int lastOrientation = Configuration.ORIENTATION_PORTRAIT;
    // 私有构造函数，防止外部实例化
    private ScreenCaptureService() {
    }

    // 提供公共的静态方法获取单例实例
    public static ScreenCaptureService getInstance() {
        return INSTANCE;
    }

    public void initContext(Context context) {
        mContext = context.getApplicationContext();
    }

    public void bindScreenService() {
        Intent intent = new Intent();
        intent.setClass(mContext, SimpleScreenShareService.class);
        mServiceConnection = new ScreenShareServiceConnection();
        mContext.bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
        Log.d("qweqwe", "bindScreenService");
    }
    public void unbindScreenService() {
        if (mServiceConnection != null) {
            mContext.unbindService(mServiceConnection);
            mServiceConnection = null;
            Log.d("qweqwe", "unbindScreenService");
        }
    }
}